package com.zb.controller;

import com.zb.dto.DeviceDTO;
import com.zb.service.DeviceService;
import com.zb.util.R;
import com.zb.vo.DeviceVO;
import com.zb.vo.Pager;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device")
@Api(value = "DeviceController", tags = {"设备控制器"})
public class DeviceController {

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/status")
    public boolean setStatus(@RequestBody DeviceVO deviceVO) {
        return deviceService.setStatus(deviceVO.getSn(), deviceVO.getStatus());
    }

    @PostMapping("/tag")
    public boolean updateDeviceTag(@RequestBody DeviceVO deviceVO) {
        return deviceService.updateDeviceTag(deviceVO.getSn(), deviceVO.getTag());
    }


    @GetMapping("/search")
    public R searchDevice(@RequestParam(value = "page", defaultValue = "1") Long page,
                          @RequestParam(value = "pageSize", defaultValue = "2") Long pageSize,
                          @RequestParam(value = "deviceId", required = false) String deviceId,
                          @RequestParam(value = "tag", required = false) String tag,
                          @RequestParam(value = "status", required = false) Integer status) {
        Pager<DeviceDTO> deviceDTOPager = deviceService.searchDevice(page, pageSize, deviceId, tag, status);
        return new R(200, deviceDTOPager);
    }
}
